#include<iostream>
#include<algorithm>
using namespace std;
int n;
int main()
{
	while (cin >> n) {
		int ans = 1e9;
		for (int i = 0; i <= 33; i++) {
			for (int j = i + 1; j <= 30; j++) {
				int k = (1 << i) + (1 << j);
				ans = min(ans, abs(k - n));
			}
		}
		cout << ans << endl;
	}
	return 0;
}